package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.wxbo.*;
import com.cskaoyan.bean.wxvo.WxCartCheckoutVo;
import com.cskaoyan.bean.wxvo.WxCartListVo;
import com.cskaoyan.service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
@RequestMapping("wx/cart/")
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    @RequestMapping("index")
    public BaseRespVo cartIndex() {
        //获得当前user

        User user = getCurrentUser();
        //调用方法
        WxCartListVo wxCartListVo = wxCartService.getCartList(user.getId());

        if (wxCartListVo != null) return BaseRespVo.ok(wxCartListVo, "成功");
        return BaseRespVo.fail("失败");

    }

    //isChecked,0 不显示，1 显示
    @RequestMapping("checked")
    public BaseRespVo cartChecked(@RequestBody WxCartCheckedBo wxCartCheckedBo) {

        User user = getCurrentUser();

        Integer[] productIds = wxCartCheckedBo.getProductIds();

        WxCartListVo wxCartListVo = wxCartService.cartChecked(user.getId(),
                productIds, wxCartCheckedBo.getIsChecked());

        if (wxCartListVo != null) return BaseRespVo.ok(wxCartListVo, "成功");
        return BaseRespVo.fail("失败");
    }

    @RequestMapping("add")
    public BaseRespVo addCart(@RequestBody WxAddCartBo wxAddCartBo) {

        User currentUser = getCurrentUser();

        int count = wxCartService.addCart(currentUser.getId(), wxAddCartBo);

        return BaseRespVo.ok(count, "成功");

    }


    @RequestMapping("delete")
    public BaseRespVo deleteCart(@RequestBody WxCartDeleteBO wxCartDeleteBO) {

        User currentUser = getCurrentUser();

        Integer[] productIds = wxCartDeleteBO.getProductIds();

        String s = Arrays.toString(productIds);
        System.out.println(s);

        boolean b = wxCartService.deleteCart(currentUser.getId(), productIds);

        WxCartListVo cartList = wxCartService.getCartList(currentUser.getId());
        if (b)
            return BaseRespVo.ok(cartList, "成功");
        return BaseRespVo.fail("失败");
    }


    @RequestMapping("update")
    public BaseRespVo updateCart(@RequestBody WxCartUpdateBo wxCartUpdateBo) {

        User currentUser = getCurrentUser();

        boolean b = wxCartService.updateCartForUpdate(wxCartUpdateBo, currentUser);

        if (b) return BaseRespVo.ok("成功");

        return BaseRespVo.fail("失败");

    }


    @RequestMapping("checkout")
    public BaseRespVo addOrder(WxCartCheckedoutBo wxCartCheckedoutBo) {

        User currentUser = getCurrentUser();

        WxCartCheckoutVo wxCartCheckoutVo = wxCartService.checkedout(wxCartCheckedoutBo, currentUser);


        if (wxCartCheckoutVo == null) {
            return BaseRespVo.fail("失败");
        }

        return BaseRespVo.ok(wxCartCheckoutVo, "成功");

    }


    @RequestMapping("goodscount")
    public BaseRespVo getGoodsCount() {
        User currentUser = getCurrentUser();
        int count = wxCartService.getGoodsGountByUserId(currentUser.getId());
        return BaseRespVo.ok(count, "成功");
    }

    @RequestMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody WxAddCartBo wxAddCartBo) {
        User currentUser = getCurrentUser();

        int count = wxCartService.fastAddCart(currentUser.getId(), wxAddCartBo);

        return BaseRespVo.ok(count, "成功");
    }

    private User getCurrentUser() {

        //获得当前user
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        return user;

    }

}
